<?php
/**************************
 * Created on Jan 22, 2011
 * By: Jos Pape
 * info: jospape@hotmail.com
 */
include_once "../../common.php";
include_once $root."/header.php";


if(isset($_POST['action']) && $_POST['action'] == "nieuwBericht")
{	// nieuwe bericht
	if(!isset($_POST['bugid']) || empty($_POST['bugid']) || !is_numeric($_POST['bugid']))
		$error['Bug ID'] = "moet verplicht worden opgegeven";
	if(!isset($_POST['Message']) || empty($_POST['Message']))
		$error['Message'] = "moet verplicht worden opgegeven";
	if(!isset($error))
	{
		$query = "INSERT INTO BugMessages (
							BugID,			 	 	 	 	 	 	
							CreateDate,			 	 	 	 	 	 	
							UserID,			 	 	 	 	 	 	
							Message
						) VALUES (
							'".str_replace("'","",$_POST['bugid'])."',			 	 	 	 	 	 	
							NOW(),			 	 	 	 	 	 	
							'".str_replace("'","",$_SESSION['login']['ID'])."',			 	 	 	 	 	 	
							'".str_replace("'","",$_POST['Message'])."'
						)";
		$_MySQL->runQuery($query);
	}
}
elseif($_POST['action'] == "updateBehandeling")
{
	if(!isset($_POST['bugid']) || empty($_POST['bugid']) || !is_numeric($_POST['bugid']))
		$error['Bug ID'] = "moet verplicht worden opgegeven";
	if(!isset($_POST['userID']) || empty($_POST['userID']) || !is_numeric($_POST['userID']))
		$error['user ID'] = "moet verplicht worden opgegeven";
	if(!isset($error))
	{
		$query = "UPDATE Bugs Set
						InBehandelingBij = '".str_replace("'","",$_POST['userID'])."'
					WHERE ID = '".str_replace("'","",$_POST['bugid'])."' 
					LIMIT 1";
		$_MySQL->runQuery($query);
	}
}
elseif(isset($_POST['action']) && $_POST['action'] == "aanmaken")
{
	if(!isset($_POST['Naam']) || empty($_POST['Naam']))
		$error['Naam'] = "moet verplicht worden opgegeven";
	if(!isset($_POST['Omschrijving']) || empty($_POST['Omschrijving']))
		$error['Omschrijving'] = "moet verplicht worden opgegeven";
	if(!isset($error))
	{
		$query = "INSERT INTO Bugs (
						Naam,
						UserID,
						CreateDate,
						Omschrijving
					) VALUES (
						'".str_replace("'","&#39;",$_POST['Naam'])."',
						'".str_replace("'","",$_SESSION['login']['ID'])."',
						NOW(),
						'".str_replace("'","&#39;",$_POST['Omschrijving'])."'
					)";
		$_MySQL->runQuery($query);
		$bugID = $_MySQL->lastInsertID();
		$query = "INSERT INTO BugVolgers (
						BugID,			 	 	 	 	 	 	
						UserID
					) VALUES (
						'".str_replace("'","",$bugID)."',
						'".str_replace("'","",$_SESSION['login']['ID'])."'
					)";
		$_MySQL->runQuery($query);
		$_GET['bug'] = $bugID;
	}
}

?>
<h1>Bugs</h1>
<?php
if(isset($error) && is_array($error))
{
	?>
	<ul class="error">
	<?php
	foreach($error AS $what => $why)
	{
		?>
		<li><b><?=$what;?></b>: <?=$why;?></li>	
		<?php	
	}
	?>
	</ul>
	<?php
}

if(isset($_GET['bug']) && is_numeric($_GET['bug']))
{
	// open bug
	$query = "	SELECT 
					Bugs.* ,
					CONCAT(Users.Firstname,' ',Users.Lastname) AS Melder,
					CONCAT(Users2.Firstname,' ',Users2.Lastname) AS BehandelingUser,
					S.Naam AS Status,
					P.Naam AS Prioriteit
				FROM Bugs 
					LEFT JOIN Users ON Users.ID = Bugs.UserID
					LEFT JOIN Users AS Users2 ON Users2.ID = Bugs.InBehandelingBij
					LEFT JOIN BugStatusen AS S ON S.ID = Bugs.StatusID
					LEFT JOIN BugPrioriteiten AS P ON P.ID = Bugs.PrioriteitID
				WHERE Bugs.ID = '".str_replace("'","",$_GET['bug'])."'";
	$result = $_MySQL->runQuery($query);
	if(mysql_num_rows($result) >=1 )
	{
		$row = mysql_fetch_assoc($result);
		?>
		<a href="?"><-- Terug naar het overzicht</a><br /><br />
		<table>
			<tr>
				<td>
					<b>Melder</b>
				</td>
				<td>:</td>
				<td>
					<?=$row['Melder'];?>
				</td>
				<td>
					<a href="pop/inbehandelingbij.php?bug=<?=$row['ID'];?>" class="openPopup" width="400" height="400" title="In Behandeling Bij">In behandeling bij</b>
				</td>
				<td>:</td>
				<td>
					<?=$row['BehandelingUser'];?>
				</td>
			</tr>
			<tr>
				<td>
					<b>Meld datum</b>
				</td>
				<td>:</td>
				<td>
					<?=getNiceDateFormat($row['CreateDate'], true);?>
				</td>
				<td>
					<b>Prioriteit</b>
				</td>
				<td>:</td>
				<td>
					<?=$row['Prioriteit'];?>
				</td>
			</tr>
			<tr>
				<td>
					<b>Status</b>
				</td>
				<td>:</td>
				<td>
					<?=$row['Status'];?>
				</td>
				<td>
					<b>Volgers</b>
				</td>
				<td>:</td>
				<td>
					<?php
					$sub_query = "	SELECT 
										CONCAT(Users.Firstname,' ',Users.Lastname) AS Volger
									FROM BugVolgers
										LEFT JOIN Users ON Users.ID = BugVolgers.UserID 
									WHERE BugID = ".$row['ID'];
					$sub_result = $_MySQL->runQuery($sub_query);
					$output = "";
					while($sub_row = mysql_fetch_assoc($sub_result))
					{
						$output .= $sub_row['Volger'].", ";
					}
					$output = substr($output, 0, -2);
					echo $output;
					?>
				</td>
			</tr>
			<tr>
				<td colspan="6" valign="top">
					<b>Omschrijving</b>:<br />
					<?=nl2br($row['Omschrijving']);?>
				</td>
			</tr>
			<tr>
				<td colspan="6" valign="top">
					<b>Berichten</b>:
					<input type="button" value="Nieuw bericht" onClick="javascript: $('#nieuwBugBericht').toggle(true);" />
					<br />
					<div id="nieuwBugBericht" style="display:none;">
						<form action="?bug=<?=$row['ID'];?>" method="post">
							<textarea name="Message" style="width:300px;height:150px;"></textarea>
							<br /><input type="submit" value=" Toevoegen " />
							<input type="hidden" name="bugid" value="<?=$row['ID'];?>" />
							<input type="hidden" name="action" value="nieuwBericht" />
						</form>
					</div>
					<?php
					$sub_query = "	SELECT
										BM.*,
										CONCAT(Users.Firstname,' ',Users.Lastname) AS BerichtGever
									FROM BugMessages AS BM
										INNER JOIN Users ON Users.ID = BM.UserID
									WHERE BM.BugID = '".str_replace("'","",$row['ID'])."'
									ORDER BY CreateDate DESC";
					$sub_result = $_MySQL->runQuery($sub_query);
					while($sub_row = mysql_fetch_assoc($sub_result))
					{
						?>
						<div style="width:90%;padding: 5px;">
							<b><?=$sub_row['BerichtGever'];?></b> (<?=getNiceDateFormat($sub_row['CreateDate'], true);?>)<br />
							<?=nl2br($sub_row['Message']);?>
						</div>
						<?php
					}
					?>
				</td>
			</tr>
		</table>
		<?php
	}
	else
	{
		?>
		<b>ERROR</b>: Deze bug bestaat niet...
		<?php
	}
}
else
{
	?>
	<input type="button" value=" Bug Aanmaken " onclick="javascript: $('#bugAanmakenForm').toggle(true);" />
	<div id="bugAanmakenForm" style="display:none;">
		<form action="?" method="post">
			<table>
				<tr>
					<td>
						Naam
					</td>
					<td>:</td>
					<td>
						<input type="text" name="Naam" />
					</td>
				</tr>
				<tr>
					<td valign="top">
						Omschrijving
					</td>
					<td valign="top">:</td>
					<td valign="top">
						<textarea name="Omschrijving" style="width:150px;height:300px;"></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="3">
						<input type="submit" value=" Aanmaken " />
						<input type="hidden" name="action" value="aanmaken" />
					</td>
				</tr>
			</table>
		</form>
	</div>
	<?php
	// open bugs for loggedin user..
	$query = "	SELECT 
					Bugs.*,
					TIMESTAMPDIFF(MINUTE, Bugs.CreateDate, NOW()) AS Duur,
					COUNT(DISTINCT BugVolgers.ID) AS AantalVolgers,
					BugPrioriteiten.Naam AS PrioriteitNaam,
					BugStatusen.Naam AS StatusNaam
				FROM Bugs 
					LEFT JOIN BugVolgers ON Bugs.ID = BugVolgers.BugID 
					LEFT JOIN BugStatusen ON Bugs.StatusID = BugStatusen.ID
					LEFT JOIN BugPrioriteiten ON Bugs.PrioriteitID = BugPrioriteiten.ID
				WHERE Bugs.InBehandelingBij = '".str_replace("'","",$_SESSION['login']['ID'])."'
					AND Bugs.StatusID != 4 /*Solved*/
					AND Bugs.StatusID != 6 /*Doubble*/
				GROUP BY Bugs.ID
				ORDER BY Bugs.CreateDate DESC";
	$result = $_MySQL->runQuery($query);
	if(mysql_num_rows($result) >= 1)
	{
		?>
		<h2>Bugs bij jou in behandeling</h2>
		<table>
			<thead>
				<tr>
					<th>ID</th>
					<th>Naam</th>
					<th>Status</th>
					<th>Prioriteit</th>
					<th>Duur</th>
					<th># Volgers</th>
				</tr>
			</thead>
			<tbody>
			<?php
			while($row = mysql_fetch_assoc($result))
			{
				?>
				<tr>
					<td><?=$row['ID'];?></td>
					<td><a href="?bug=<?=$row['ID'];?>"><?=$row['Naam'];?></a></td>
					<td><?=$row['StatusNaam'];?></td>
					<td><?=$row['PrioriteitNaam'];?></td>
					<td><?=getNiceDuur($row['Duur']);?></td>
					<td><?=$row['AantalVolgers'];?></td>
				</tr>
				<?php
			}
			?>
			</tbody>
		</table>
		<?php
	}
	
	// open bugs by loggedin user..
	$query = "	SELECT 
					Bugs.*,
					TIMESTAMPDIFF(MINUTE, Bugs.CreateDate, NOW()) AS Duur,
					COUNT(DISTINCT BugVolgers.ID) AS AantalVolgers,
					BugPrioriteiten.Naam AS PrioriteitNaam,
					BugStatusen.Naam AS StatusNaam 
				FROM Bugs
					LEFT JOIN BugVolgers ON BugVolgers.BugID = Bugs.ID 
					LEFT JOIN BugStatusen ON Bugs.StatusID = BugStatusen.ID
					LEFT JOIN BugPrioriteiten ON Bugs.PrioriteitID = BugPrioriteiten.ID
				WHERE (
						Bugs.UserID = '".str_replace("'","",$_SESSION['login']['ID'])."'
						OR 
						BugVolgers.UserID = '".str_replace("'","",$_SESSION['login']['ID'])."'
					) AND Bugs.StatusID != 4 /*Solved*/
					AND Bugs.StatusID != 6 /*Doubble*/
				GROUP BY Bugs.ID";
	$result = $_MySQL->runQuery($query);
	if(mysql_num_rows($result) >= 1)
	{
		?>
		<h2>Bugs door jou aangemeld of die je volgd</h2>
		<table>
			<thead>
				<tr>
					<th>ID</th>
					<th>Naam</th>
					<th>Status</th>
					<th>Prioriteit</th>
					<th>Duur</th>
					<th># Volgers</th>
				</tr>
			</thead>
			<tbody>
			<?php
			while($row = mysql_fetch_assoc($result))
			{
				?>
				<tr>
					<td><?=$row['ID'];?></td>
					<td><a href="?bug=<?=$row['ID'];?>"><?=$row['Naam'];?></a></td>
					<td><?=$row['StatusNaam'];?></td>
					<td><?=$row['PrioriteitNaam'];?></td>
					<td><?=getNiceDuur($row['Duur']);?></td>
					<td><?=$row['AantalVolgers'];?></td>
				</tr>
				<?php
			}
			?>
			</tbody>
		</table>
		<?php
	}
}

include_once $root."/footer.php";
?>